<?php

require_once __DIR__."/../config/defines.php";

define("__INDEX__", true);

require_once __DIR__."/../config/config.php";

if(defined("WC_NOT_INSTALLED")) {
	header("Location: ../");
} else {
	if(isset($_REQUEST["service"]) && trim($_REQUEST["service"]) != "") {
		$wcServiceManager = WCServicesManager::I();
		$wcService = $wcServiceManager->currentService();
		$json = $wcService->getInterface();

		echo "<h4>Service: '{$_REQUEST["service"]}'</h4>\n<hr/>\n";
		echo "<pre>\n";
		echo "$.ajax({\n";
		echo "\tdataType: \"json\",\n";

		$surl = "service.php?service={$_REQUEST["service"]}";

		foreach($json->params->url as $param) {
			if($param == "group") {
				$surl.= "&group=\" + GROUP_ID + \"";
			} else {
				$surl.= "&$param=\" + XXXXXXXX + \"";
			}
		}
		echo "\turl: \"{$surl}\",\n";

		$method = array_shift($json->methods);
		echo "\ttype: \"{$method}\",\n";

		if(count($json->params->params)) {
			echo "\tdata: {\n";
			echo "\t\tparams: JSON.stringify({\n";
			$first = true;
			foreach($json->params->params as $param) {
				if(!$first) {
					echo ",\n";
				}
				echo "\t\t\t{$param}: XXXXXXXX";
				$first = false;
			}
			echo "\n\t\t})\n";
			echo "\t},\n";
		}

		echo "\tsuccess: function(response) {\n";
		echo "\t\tif(typeof response.status !== \"undefined\" && response.status) {\n";
		echo "\t\t\tconsole.log(response.data);\n";
		echo "\t\t} else {\n";
		echo "\t\t\tconsole.log(response);\n";
		echo "\t\t}\n";
		echo "\t},\n";

		echo "\terror: function(response) {\n";
		echo "\t\tconsole.log(response);\n";
		echo "\t}\n";

		echo "});\n";
		echo "</pre>\n";
	} else {
		echo "service?";
	}
}
